package cn.wsalix.login.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.wsalix.biz.service.CategoryService;
import cn.wsalix.constant.Global;
import cn.wsalix.login.entity.QQUser;
import cn.wsalix.login.form.QQUserForm;
import cn.wsalix.login.repository.QQUserRepository;
import cn.wsalix.login.service.QQUserService;
import cn.wsalix.observable.service.LoginService;
import cn.wsalix.shiro.token.QQToken;
import cn.anlaser.utils.FastJsonMapper;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;

import com.qq.open.OpenApiV3;
import com.qq.open.OpensnsException;

@Transactional
@Service("qqUserService")
public class QQUserServiceImpl extends
		SimpleServiceImpl<QQUserForm, QQUserRepository, QQUser> implements
		QQUserService {
	@Autowired
	private QQUserRepository qqUserRepository;
	@Autowired
	private CategoryService categoryService;

	@Override
	public String module() {
		return "/qq/user";
	}

	@Override
	public QQUserRepository getRepository() {
		return qqUserRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(QQUserForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getUserId() != null && form.getUserId() != 0) {
			filters.put("user.id", new SearchFilter("user.id", Operator.EQ,
					form.getUserId()));
		}
		if (form.getOpenId() != null && !form.getOpenId().equals("")) {
			filters.put("openid", new SearchFilter("openid", Operator.EQ,
					form.getOpenId()));
		}
		return filters;
	}

	@Override
	public Class<QQUser> getEntityClazz() {
		return QQUser.class;
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof LoginService) {
			// UserLoginService userLoginService = (UserLoginService) o;
			if (arg instanceof QQToken) {
				QQToken upToken = (QQToken) arg;
				QQUser qqUser = getQQInfo(upToken);
				qqUser = login(qqUser);
				upToken.setQqUser(qqUser);
			}
		}

	}

	private QQUser login(QQUser qqUser) {
		qqUser = qqUserRepository.findByOpenId(qqUser.getOpenId());
		if (qqUser.getId() == null) {
			qqUser = add(qqUser);
		} else {
			
		}
		return qqUser;
	}

	private QQUser getQQInfo(QQToken upToken) {
		OpenApiV3 sdk = new OpenApiV3(Global.qqAppId, Global.qqAppKey);
		String pf = "qzone";

		sdk.setServerName(Global.qqServerName);

		String scriptName = "/v3/user/get_info";

		// 指定HTTP请求协议类型
		String protocol = "http";

		// 填充URL请求参数
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("openid", upToken.getOpenId());
		params.put("openkey", upToken.getAccessToken());
		params.put("pf", pf);

		try {
			String resp = sdk.api(scriptName, params, protocol);
			System.out.println(resp);
			QQUser qqUser = FastJsonMapper.fromJson(resp, QQUser.class);
			qqUser.setOpenId(upToken.getOpenId());
			return qqUser;
		} catch (OpensnsException e) {
			System.out.printf("Request Failed. code:%d, msg:%s\n",
					e.getErrorCode(), e.getMessage());
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

}
